﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using DAL.DBManagers;
using DAL.Models;

public partial class ServiceEdit : System.Web.UI.Page
{
    Guid ServiceID = Guid.Empty;
    ServicesManager manager = new ServicesManager();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Guid.TryParse(Request.QueryString["id"], out ServiceID))
        {
            if (!IsPostBack)
            {
                var service = manager.Single(s => s.ServiceID == ServiceID);

                txtName.Text = service.Name;
                //txtNameGeo.Text = service.NameGeo;
                ddlCurrencyTypes.SelectedValue = service.CurrencyTypeID.ToString();
                txtUnitPrice.Text = service.UnitPrice.ToString();
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        Page.Validate();
        if (!Page.IsValid)
            return;

        if (ServiceID == Guid.Empty)
        {
            manager.Add(new Service
            {
                ServiceID = Guid.NewGuid(),
                Name = txtName.Text,
                //NameGeo = txtNameGeo.Text,
                CurrencyTypeID = Guid.Parse(ddlCurrencyTypes.SelectedValue),
                InvoiceID = Guid.Empty,
                UnitPrice = decimal.Parse(txtUnitPrice.Text),
                IsActive = true,
                CreateDate = DateTime.Now,
                CreatedBy = (Session["User"] as UserHelper).UserID
            });
        }
        else
        {
            var service = manager.Single(s => s.ServiceID == ServiceID);

            service.Name = txtName.Text;
            //service.NameGeo = txtNameGeo.Text;
            service.CurrencyTypeID = Guid.Parse(ddlCurrencyTypes.SelectedValue);
            service.InvoiceID = Guid.Empty;
            service.UnitPrice = decimal.Parse(txtUnitPrice.Text);
            service.LastModifyDate = DateTime.Now;
            service.LastModifier = (Session["User"] as UserHelper).UserID;
        }
        manager.SaveChanges();
        Response.Redirect("serviceview.aspx?m=liService");
    }
}